package com.tronsis.library.util;

import android.content.Context;
import android.os.CountDownTimer;
import android.widget.TextView;


public class CountDownTimerShareUtil extends CountDownTimer {
    private TextView mTextView;
    private Context mContext;
    private String mHeaderText;
    private String mFinishText;

    public CountDownTimerShareUtil(TextView textView, String headerText, String finishText, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        mTextView = textView;
        mContext = textView.getContext();
        mHeaderText = headerText;
        mFinishText = finishText;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        setText(millisUntilFinished);
    }

    @Override
    public void onFinish() {
        mTextView.setText(mFinishText);
    }

    public void setText(long mills) {
        int day = 0;
        int hour;

        day = (int) (mills / (1000 * 60 * 60 * 24));
        hour = (int) ((mills % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
//        hour = (int) (mills / (1000 * 60 * 60));
        int minute = (int) ((mills % (1000 * 60 * 60)) / (1000 * 60));
        int second = (int) ((mills % (1000 * 60)) / 1000);

        String dayStr = "";
        if (day != 0) {
            dayStr = day + "天";
        }
        String hourStr = "";
        if (hour != 0) {
            hourStr = hour + "小时";
        }
        String minuteStr = "";
        if (minute != 0) {
            minuteStr = minute + "分钟";
        }
        String secondStr = "";
        if (second != 0) {
            secondStr = second + "秒";
        }

        String msg = mHeaderText + dayStr + hourStr + minuteStr + secondStr;
        mTextView.setText(msg);
    }
}